package com.example.websocket_service.controller;


import com.example.websocket_service.config.RestResponse;
import com.example.websocket_service.entity.Message;
import com.example.websocket_service.entity.form.UserServiceF;
import com.example.websocket_service.entity.vo.UserVo;

import com.example.websocket_service.service.FeignService;
import com.example.websocket_service.service.MessageService;
import io.lettuce.core.dynamic.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MessageService messageService;


    /**
     * 获取用户信息
     */
    @GetMapping
    public RestResponse getUserList() {
        List<UserVo> userList = new LinkedList<>();
        Set<String> keys = redisTemplate.keys("*");
        for (String key : keys) {
            UserVo userVo = new UserVo();
            String userName = key.split("_")[2];
            Double score = redisTemplate.opsForZSet().score(key, userName);
            if (Objects.nonNull(score)) {
                Date date = new Date(score.longValue());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String createTime = format.format(date);
                String roomId = key.split("_")[1];
                userVo.setRoomId(Integer.valueOf(roomId));
                userVo.setUserName(userName);
                userVo.setCreatedTime(createTime);
                userList.add(userVo);
            }
        }
        return new RestResponse().put(userList);
//        return ApiResult.ok().data(userList);
    }

    @GetMapping("/recordList")
    public RestResponse getUserServiceList(@Valid @ModelAttribute UserServiceF form) {
//        RestResponse user = feignService.user();
//        System.out.println(user);
        return new RestResponse().put(messageService.getServiceList(form));
    }
}

